package com.zhao.dota.processor;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.zhao.dota.common.processor.SteamProcessor;
import com.zhao.dota.struct.SteamResponse;
import lombok.Data;
import okhttp3.Request;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class PlayerSummariesProcessor extends SteamProcessor {

    private static final String URL = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/";

    public List<Player> apply(String... steamIds) {
        Request request = super.buildGet(URL, new Param("steamids", StringUtils.join(steamIds, ",")));
        String result = super.sendRequest(request);
        SteamResponse<Container> response = super.readValue(result, new TypeReference<SteamResponse<Container>>() {
        });
        return response.getResponse().getPlayers();
    }

    public List<Player> apply(Collection<?> steamIds) {
        Request request = super.buildGet(URL, new Param("steamids", StringUtils.join(steamIds, ",")));
        String result = super.sendRequest(request);
        SteamResponse<Container> response = super.readValue(result, new TypeReference<SteamResponse<Container>>() {
        });
        return response.getResponse().getPlayers();
    }

    @Data
    public static class Container {

        private List<Player> players;

    }

    @Data
    public static class Player {

        @JsonProperty("steamid")
        private Long steamId;

        @JsonProperty("communityvisibilitystate")
        private int communityVisibilityState;

        @JsonProperty("profilestate")
        private int profileState;

        @JsonProperty("personaname")
        private String personaName;

        @JsonProperty("profileurl")
        private String profileUrl;

        private String avatar;
        @JsonProperty("avatarmedium")
        private String avatarMedium;

        @JsonProperty("avatarfull")
        private String avatarFull;

        @JsonProperty("avatarhash")
        private String avatarHash;

        @JsonProperty("personastate")
        private int personaState;

        @JsonProperty("primaryclanid")
        private String primaryClanId;

        @JsonProperty("personastateflags")
        private int personaStateFlags;

        @JsonProperty("loccountrycode")
        private String locCountryCode;

        @JsonProperty("locstatecode")
        private String locStateCode;

        @JsonProperty("loccityid")
        private int locCityId;

        @JsonProperty("commentpermission")
        private int commentPermission;

        @JsonProperty("lastlogoff")
        private long lastLogoff;

        @JsonProperty("realname")
        private String realName;

        @JsonProperty("timecreated")
        private long timeCreated;

    }

}
